In JavaScript, variables can be declared using var
, let
, or const
. Each
keyword has its own scope and usage.
var
The var
keyword declares a variable that is function-scoped or globally-scoped.
var x = 5;
console.log(x); // Outputs: 5
let
The let
keyword declares a block-scoped variable, which means it is only accessible within the block
it is declared.
let y = 10;
console.log(y); // Outputs: 10
const
The const
keyword declares a block-scoped variable that cannot be reassigned after its initial
declaration.
const z = 15;
console.log(z); // Outputs: 15